from pymongo import MongoClient
import csv
import codecs

FILE = "D:\\prescription.csv"
# 源client
client = MongoClient('mongodb://admin:yzy%40123456@10.11.16.1:27017/')
# 目标数据库
target_db = client["cm_data"]
# 源document
origin_collection = target_db["prescription"]

# 原始数据总数
count = origin_collection.count_documents({"status":1})
print("原始数据总数:")
print(count)

datas = origin_collection.find({"status":1})
with codecs.open(FILE, 'a', encoding='utf-8') as csvfile:
    writer = csv.writer(csvfile)
    # 设置csv头行，选择要导出的字段
    writer.writerow(["name","typeName"])
    for data in datas:
        writer.writerow([data['name'],data['typeName']])


        生姜